home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2.sit
/
Raven 1.2
/
Source
/
Foundation
/
Common
/
ZReferenceCounted.cpp
< prev
next >
Wrap
Text File
|
1996-10-28
|
2KB
|
95 lines
/*
* File: ZReferenceCounted.h
* Summary: Mixin class to help with reference counting.
* Written by: Jesse Jones
*
* Copyright ゥ 1996 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <-> 1/14/96 JDJ Created.
*/
#include <ZReferenceCounted.h>
//---------------------------------------------------------------
//
// MReferenceCounted::~MReferenceCounted
//
//---------------------------------------------------------------
MReferenceCounted::~MReferenceCounted()
{
// It's legal to delete an object with a ref count of one so
// that MReferenceCounted objects can be used on the stack.
ASSERT(mRefCount <= 1);
}
//---------------------------------------------------------------
//
// MReferenceCounted::MReferenceCounted
//
//---------------------------------------------------------------
MReferenceCounted::MReferenceCounted(long refCount)
{
ASSERT(refCount >= 0);
mRefCount = refCount;
}
//---------------------------------------------------------------
//
// MReferenceCounted::AddReference
//
//---------------------------------------------------------------
void MReferenceCounted::AddReference()
{
ASSERT(mRefCount >= 0);
if (++mRefCount == 1)
this->OnFirstReference();
}
//---------------------------------------------------------------
//
// MReferenceCounted::RemoveReference
//
//---------------------------------------------------------------
void MReferenceCounted::RemoveReference()
{
ASSERT(mRefCount >= 1);
if (--mRefCount == 0)
this->OnLastReference();
}
//---------------------------------------------------------------
//
// MReferenceCounted::OnFirstReference
//
//---------------------------------------------------------------
void MReferenceCounted::OnFirstReference()
{
ASSERT(mRefCount == 1);
}
//---------------------------------------------------------------
//
// MReferenceCounted::OnLastReference
//
//---------------------------------------------------------------
void MReferenceCounted::OnLastReference()
{
ASSERT(mRefCount == 0);
delete this;
}